package chatsystem.gui;

import chatsystem.FileTransferStatusCB;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JOptionPane;


/** Cette fenêtre apparait lors de la réception d'un fichier, et reste pendant
 *  la durée du transfert. */
public class FileRecvDialog extends javax.swing.JFrame
                            implements WindowListener
{

   /**
    * Creates new form FileRecvDialog
    */
   public FileRecvDialog(FromGUI b, String peer, String filename) 
   {
      _b = b;
      _inprogress = false;
      _started = false;
      
      initComponents();
      this.addWindowListener(this);
      
      this.doLayout();
      this.setLayout(null);
      
      lPeerName.setText(peer);
      lFileName.setText(filename);
      
      this.setVisible(true);
   }

   /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
   @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      bAbort = new javax.swing.JButton();
      bAccept = new javax.swing.JButton();
      lPeerName = new javax.swing.JLabel();
      lSends = new javax.swing.JLabel();
      lFileName = new javax.swing.JLabel();

      setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
      setName("File Recepetion"); // NOI18N
      setResizable(false);

      bAbort.setText("Abort");
      bAbort.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            bAbortActionPerformed(evt);
         }
      });

      bAccept.setText("Accept");
      bAccept.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            bAcceptActionPerformed(evt);
         }
      });

      lPeerName.setText("<user name>");

      lSends.setText("wishes to send you the following file:");

      lFileName.setText("<file name>");

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(lSends, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
               .addGroup(layout.createSequentialGroup()
                  .addComponent(bAccept, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                  .addComponent(bAbort, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE))
               .addComponent(lFileName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
               .addComponent(lPeerName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap())
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(lPeerName)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(lSends)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(lFileName)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(bAccept)
               .addComponent(bAbort))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

   private void bAcceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAcceptActionPerformed
      _inprogress = true;
      _started = true;
      _b.acceptFile(true, new FileTransferStatusCB () {
         public void onStart() {
            bAccept.setText("In Progress...");
         }
         public void onFinish() {
            bAccept.setText("Finished !");
            bAbort.setText("Exit");
            _inprogress = false;
         }
         public void onError(String msg) {
            _inprogress = false;
            JOptionPane.showMessageDialog(null, "An error occured during the transfer.",
                                   "Transfer error", JOptionPane.ERROR_MESSAGE);
            bAbort.setText("Exit");
         }         
      });
      bAccept.setText("Starting...");
      bAccept.setEnabled(false);
   }//GEN-LAST:event_bAcceptActionPerformed

   private void bAbortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAbortActionPerformed
      exit();
   }//GEN-LAST:event_bAbortActionPerformed

   public void windowClosing(WindowEvent e) {
      // s'assurer que la requête du NI reçoit une réponse dans tous les cas.
      exit();
   }
   
   private void exit()
   {
      if (_inprogress)
         _b.abortFileRecv();
      else if (!_started)
         _b.acceptFile(false, null);
      
      this.dispose();
   }
   
   private FromGUI _b;
   private boolean _inprogress;
   private boolean _started;

   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton bAbort;
   private javax.swing.JButton bAccept;
   private javax.swing.JLabel lFileName;
   private javax.swing.JLabel lPeerName;
   private javax.swing.JLabel lSends;
   // End of variables declaration//GEN-END:variables

   public void windowOpened(WindowEvent e) {}
   public void windowClosed(WindowEvent e) {}
   public void windowIconified(WindowEvent e) {}
   public void windowDeiconified(WindowEvent e) {}
   public void windowActivated(WindowEvent e) {}
   public void windowDeactivated(WindowEvent e) {}
}
